﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyIni;

namespace CadStrategy2013.CgqStyle
{
    public class DsDlClass : Cgq2013
    {
        string m_ceng;
        int m_bNo;
        public DsDlClass(string secname)
            : base(secname)
        {
        }
        public DsDlClass(string secname, Dictionary<string, string> dic)
            :base(secname,dic)
        {
        }
        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("灯丝电流");
        //}
        public override List<string> ReadTemplateDxf()
        {
            IniFile inifile = new IniFile("./\\" + StationName + "\\xhj.txt");
           // 智能灯丝=1
            inifile.Load();
            int i = inifile.GetInt("信号机", "智能灯丝", 0);
            if(i==0)
               return ReadDxf(Path + "\\template\\灯丝电流.dll");
            else
                return ReadDxf(Path + "\\template\\智能灯丝电流.dll");
        }
        public override string GetSaveFileName(int index)
        {
            if (ShiJiTuZhiCount == 1)
                return "\\灯丝电流.dxf";
            return "\\灯丝电流" + (index + 1).ToString() + ".dxf";
            //if (index == 0)
            //    return "\\灯丝电流.dxf";
            //else
            //    return "\\灯丝电流" + index + ".dxf";
        }
        public override DxfDataClass GetDxfData() //override
        {
            DxfDataClass dxfDataClass = new DxfDataClass();

            IniFile inifile = new IniFile("./\\" + StationName + "\\xhj.txt");
            string strtemp = inifile.GetString("信号机", "板号", "");
            try
            {
                int l = strtemp.IndexOf('-');
                m_ceng = strtemp.Substring(0, l);
                int.TryParse(strtemp.Substring(l + 1, strtemp.Length - l - 1), out m_bNo);
            }
            catch
            {
                m_ceng = "";
                m_bNo = 0;
            }
            GetFx_Fx31(dxfDataClass, inifile);
            GetFx02(dxfDataClass, inifile);
            GetOther(dxfDataClass, inifile);
            GetUm71(dxfDataClass, inifile);
            GetOneDj(dxfDataClass, inifile);
            ShiJiBanCount = dxfDataClass.devList.Count/ BanDevCount;
            ShiJiTuZhiCount = (ShiJiBanCount-1) / BanCount + 1;
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }
        //public override string ReplaceDev(string str2, int fi)
        //{
        //    string str = str2;
        //    for (int j = 1; j <= BanCount; j++)
        //    {
        //        int nj = j;//- fi * BanCount;
        //        for (int i = 1; i <= BanDevCount; i++)
        //        {
        //            string str1 = "dev" + nj.ToString() + "-" + i.ToString() + ">";
        //            if (str.Contains(str1))
        //            {
        //                int index = (j - 1) + fi * BanCount;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    return "";
        //                try
        //                {
        //                    return str = str.Replace(str1, m_dxfDataClass.devList[index].DevName);
        //                }
        //                catch (Exception e)
        //                {
        //                    return "";
        //                }
        //            }
        //        }
        //    }
        //    return str;
        //}
        public override string ReplacePei(string str, int fi)
        {
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;
                for (int i = 1; i <= BanDevPeiCount ; i++)
                {
                    string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (j - 1)*BanDevCount+(i-1)/2 + fi * BanCount*BanDevCount;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        int index1 = (i - 1) % BanDevCount;
                        if (index1 >= m_dxfDataClass.devList[index].peiList.Count)
                            return "";
                        try
                        {
                            return str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
                        }
                        catch 
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }
        public override string ReplacebNo(string str, int fi = 0)
        {
            //return base.ReplacebNo(str, fi);
            string str1 = str;

            string str2 = "CX-Y";
            if (str.Contains(str2))
            {
                if (ShiJiBanCount == 0)
                {
                    string s = str;
                    return s.Replace(str2, "");
                }
                return str.Replace(str2, GetCengNo(0, fi));
            }

            return str1;
        }
        private string GetCengNo(int n, int fi)
        {
            int cengNo;
            int.TryParse(m_ceng.Substring(1, m_ceng.Length - 1), out cengNo);
            int m = n + fi;// *BanCount;
            if (m > ShiJiBanCount)
                return "";
            int n1 = m + m_bNo;
            if (m_ceng == "C1")
            {
                if (n1 >= 8)
                {
                    int cn = (n1 - 8) / 8 + 2;
                    int ws = (n1 - 8) % 8 + 2;
                    return "C" + cn + "-" + ws;
                }
                else
                    return "C1-" + n1;
            }
            else
            {
                int cn = (n1 - 2) / 8 + cengNo;
                int ws = (n1 - 2) % 8 + 2;
                return "C" + cn + "-" + ws;
            }
        }
        public override List<string> DealData(List<string> strList, int fi)
        {
            List<string> list = new List<string>();
            int n = 0;
            foreach (string str in strList)
            {
                string str1 = str;
                str1 = ReplaceStationName(str1);
                str1 = ReplaceDev(str1, fi);
                str1 = ReplacePei(str1, fi);
                str1 = ReplacebNo(str1, fi);
                list.Add(str1);
                OnDealData(this, new WriteDxfArgs(SecName + fi.ToString(), str, strList.Count, ++n));
            }
            return list;
        }

        public override void GetCfg(string strSec)
        {
            IniFile inifile = new IniFile("./\\" + StationName + "\\xhj.txt");
            inifile.Load();
            int i = inifile.GetInt("信号机", "智能灯丝", 0);
            if (i == 0)
                base.GetCfg(strSec);
            else
                base.GetCfg("智能灯丝");
        }

        private void GetFx_Fx31(DxfDataClass dxfDataClass, IniFile inifile)
        {
            string[] djName = { "-1DJ电流", "-2DJ电流" };
            string[] strSecs = new string[] { "信号机JX", "信号机3FX31", "信号机JX1_JX2", "信号机FX12", "信号机2FX12" };
            foreach (string strSec in strSecs)
            {
                int sum0 = inifile.GetInt16(strSec, "sum", 0);
                string str1s = inifile.GetString(strSec, "定型配线", "");
                string[] strDingXings = str1s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < sum0; i++)
                {
                    string strs = inifile.GetString(strSec, (i + 1).ToString(), "");
                    string[] otherData = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < djName.Length; j++)
                    {
                        DevClass devClass = new DevClass();

                        devClass.DevName = otherData[0]+djName[j];
                    
                        try
                        {
                            devClass.peiList.Add(otherData[1] + strDingXings[2*j]);
                            devClass.peiList.Add(otherData[1] + strDingXings[1+2*j]);
                        }
                        catch
                        {
                            devClass.peiList.Add("");
                            devClass.peiList.Add("");
                        }
                        dxfDataClass.devList.Add(devClass);
                    }
                }
            }
        }
        private void GetFx02(DxfDataClass dxfDataClass, IniFile inifile)
        {
            string[] djName = { "-1DJ电流", "-2DJ电流" };
            int sum0 = inifile.GetInt16("信号机3FX02", "sum", 0);
            string str1s = inifile.GetString("信号机3FX02", "定型配线A", "");
            string[] strDingXingsA = str1s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string str2s = inifile.GetString("信号机3FX02", "定型配线B", "");
            string[] strDingXingsB = str2s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("信号机3FX02", (i + 1).ToString(), "");
                string[] otherData = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int k = 0; k < djName.Length; k++)
                {
                    DevClass devClass = new DevClass();
                    devClass.DevName = otherData[0] + djName[k];
                    try
                    {
                        if (otherData[2] == "A")
                        {
                            devClass.peiList.Add(otherData[1] + strDingXingsA[0 + k * 2]);
                            devClass.peiList.Add(otherData[1] + strDingXingsA[1 + k * 2]);
                        }
                        else
                        {
                            devClass.peiList.Add(otherData[1] + strDingXingsB[0 + k * 2]);
                            devClass.peiList.Add(otherData[1] + strDingXingsB[1 + k * 2]);
                        }
                    }
                    catch
                    {
                        devClass.peiList.Add("");
                        devClass.peiList.Add("");
                    }
                    dxfDataClass.devList.Add(devClass);
                }
            }
        }
        private void GetOther(DxfDataClass dxfDataClass, IniFile inifile)
        {
            string[] djName = { "-1DJ电流", "-2DJ电流" };
            int sum0 = inifile.GetInt16("其他信号机", "sum", 0);
            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("其他信号机", (i + 1).ToString(), "");
                string[] otherData = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < djName.Length; j++)
                {
                    DevClass devClass = new DevClass();
                    devClass.DevName = otherData[0] + djName[j];
                    try
                    {
                        devClass.peiList.Add(otherData[1 + j * 2]);
                        devClass.peiList.Add(otherData[2 + j * 2]);
                    }
                    catch
                    {
                        devClass.peiList.Add("");
                        devClass.peiList.Add("");
                    }
                    dxfDataClass.devList.Add(devClass);
                }
            }
        }
        private void GetOneDj(DxfDataClass dxfDataClass, IniFile inifile)
        {
            string[] djName = { "-DJ电流" };
            int sum0 = inifile.GetInt16("单个DJ信号机", "sum", 0);
            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("单个DJ信号机", (i + 1).ToString(), "");
                string[] otherData = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < djName.Length; j++)
                {
                    DevClass devClass = new DevClass();
                    devClass.DevName = otherData[0] + djName[j];
                    try
                    {
                        devClass.peiList.Add(otherData[1]);
                        devClass.peiList.Add(otherData[2]);
                    }
                    catch
                    {
                        devClass.peiList.Add("");
                        devClass.peiList.Add("");
                    }
                    dxfDataClass.devList.Add(devClass);
                }
            }
        }
        private void GetUm71(DxfDataClass dxfDataClass, IniFile inifile)
        {
            string[] strName = new string[] { "-L电流", "-H电流", "-U电流" };
            int sum0 = inifile.GetInt16("区间信号机UM71", "sum", 0);
            string str1s = inifile.GetString("区间信号机UM71", "定型配线", "");
            string[] strDingXings = str1s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < sum0; i++)
            {
                string strs = inifile.GetString("区间信号机UM71", (i + 1).ToString(), "");
                string[] otherData = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < strName.Length; j++)
                {
                    DevClass devClass = new DevClass();
                    devClass.DevName = otherData[0] + strName[j];
                    try
                    {
                        devClass.peiList.Add(otherData[1] + strDingXings[j]);
                        devClass.peiList.Add(otherData[j + 2]);
                    }
                    catch
                    {
                        devClass.peiList.Add("");
                        devClass.peiList.Add("");
                    }
                    dxfDataClass.devList.Add(devClass);
                }
                DevClass devClass1 = new DevClass();
                devClass1.DevName = "";
                try
                {
                    devClass1.peiList.Add("");
                    devClass1.peiList.Add("");
                }
                catch
                {
                    devClass1.peiList.Add("");
                    devClass1.peiList.Add("");
                }
                dxfDataClass.devList.Add(devClass1);
            }
        }
    }
}
